/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.informet.ipd.modelo.dao;

import com.informet.ipd.modelo.vo.AvancesPorActividadVO;



import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author LAPTOP01
 */
public class AvancePorActividadDao extends DBase{


  Connection conn = null;

    public AvancePorActividadDao() {
        super();
    }

 
    public void  ListActividadesPer(AvancesPorActividadVO bean, ResultSet rs) throws SQLException{
        //dtIniactc dtTeractc

           bean.setNUM_CODACTPER_PK(rs.getInt("NUM_CODACTPER_PK"));
           //bean.setDT_INIACTPER(rs.getString("DT_INIACTPER"));

           bean.setDT_INIACTPERC(rs.getString("dtIniactc"));
           bean.setDT_TERACTPERC(rs.getString("dtTeractc"));
            bean.setDT_TERACTPER(rs.getString("dtTeract"));
            bean.setDT_INIACTPER(rs.getString("dtIniact"));
          // bean.setDT_TERACTPER(rs.getString("DT_TERACTPER"));

          // bean.setDT_INIACTPERC(rs.getString("DT_INIACTPERC"));           
           //bean.setDT_TERACTPERC(rs.getString("DT_TERACTPERC"));
         
           bean.setTX_DESCOBJ(rs.getString("TX_DESCOBJ"));
           bean.setTX_DESCOBJESP(rs.getString("TX_DESCOBJESP"));
           bean.setTX_DESCACTPER(rs.getString("TX_DESCACTPER"));
           bean.setTX_OBSERVACION(rs.getString("TX_OBSERVACION"));

          //Ini -->
           bean.setNUM_CODACTPER_PK(Integer.parseInt(rs.getString("NUM_CODACTPER_PK")));
           bean.setNumCodObj(Integer.parseInt(rs.getString("NUM_CODOBJ_PK")));
           bean.setNUM_CODOBJESP_PK(Integer.parseInt(rs.getString("NUM_CODOBJESP_PK")));

           bean.setCodEtiqueta(Integer.parseInt(rs.getString("codEtiqueta")));
          //Fin -->
    }


    public void  setEtiquetas(AvancesPorActividadVO bean, ResultSet rs) throws SQLException{

           bean.setCodEtiqueta(Integer.parseInt(rs.getString("NUM_CODETIQUETACTPER_PK")));
           bean.setDescEtiqueta(rs.getString("tx_descetiquet"));

    }

    public List getListEtiquetas(String id,String trimestre)  throws SQLException {
        Statement stmt = null;
        ResultSet rs = null;
        ArrayList<AvancesPorActividadVO> lista = new ArrayList<AvancesPorActividadVO>(0);

        String sql = "";
        try {
            conn = DriverManager.getConnection(url, usuario, password);
            stmt = conn.createStatement();

                sql = "  select * from ME_etiquetactper ";


            rs = stmt.executeQuery(sql);
            AvancesPorActividadVO bean = null;
            while (rs.next()) {
                bean = new AvancesPorActividadVO();
                setEtiquetas(bean,rs);
                lista.add(bean);
            }

        } finally {
            if (rs != null) {
                rs.close();
                rs = null;
            }
            if (stmt != null) {
                stmt.close();
                stmt = null;
            }
            if (conn != null) {
                conn.close();
                conn = null;
            }

        }
        return lista;
    }

  public List getListActividadesPer(String id)  throws SQLException {
        Statement stmt = null;
        ResultSet rs = null;
        ArrayList<AvancesPorActividadVO> lista = new ArrayList<AvancesPorActividadVO>(0);
        
        String sql = "";
        try {
            conn = DriverManager.getConnection(url, usuario, password);
            stmt = conn.createStatement();          
//                 sql = " select * from me_actper "+
//                       " where 1=1 ";
                   sql = "  select  "+

                         "      isnull(convert(varchar,a.DT_INIACTPER,103),'') as dtIniact, "+
                         "      isnull(convert(varchar,a.DT_TERACTPER,103),'') as dtTeract  , "+

                         "      isnull(convert(varchar,a.DT_INIACTPERC,103),'') as dtIniactc, "+
                         "      isnull(convert(varchar,a.DT_TERACTPERC,103),'') as dtTeractc  , "+
                         "      isnull(a.NUM_CODETIQUETACTPER_PK,0) as codEtiqueta, "+
                         "      * "+
                         "  from me_actper a inner join me_objesp os on os.num_codobjesp_pk=a.num_codobjesp_pk  "+
                         "                  inner join me_obj o on o.num_codobj_pk=os.num_codobj_pk  "+
                    "  where 1=1 ";
                 if(id!=null)
                  sql=sql+ " and a.num_codactper_pk="+id;

            rs = stmt.executeQuery(sql);
            AvancesPorActividadVO bean = null;
            while (rs.next()) {
                bean = new AvancesPorActividadVO();
                ListActividadesPer(bean,rs);
                lista.add(bean);
            }

        } finally {
            if (rs != null) {
                rs.close();
                rs = null;
            }
            if (stmt != null) {
                stmt.close();
                stmt = null;
            }
            if (conn != null) {
                conn.close();
                conn = null;
            }

        }
        return lista;
    }


      public String spMan(AvancesPorActividadVO bean) {
        String msj = "";
        
        try {
        	
            conn = DriverManager.getConnection(url, usuario, password);
            CallableStatement cs = conn.prepareCall("{call Man_update_MeActPerFecEjecucion(?,?,?,?,?,?,?)}");
            cs.setInt(1, bean.getNUM_CODACTPER_PK());
            cs.setString(2, bean.getDT_INIACTPERC());
            cs.setString(3, bean.getDT_TERACTPERC());
            cs.setString(4, bean.getTipoOper());
            cs.setInt(5, bean.getCodEtiqueta());
            cs.setString(6, bean.getDescEtiqueta());
            cs.setString(7, bean.getTX_OBSERVACION());

            cs.execute();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.toString());
            msj = e.toString();
            return msj;
        }
        return msj;
    }

      public String  spManDelete(AvancesPorActividadVO bean) {
        String msj = "";

        try {

            conn = DriverManager.getConnection(url, usuario, password);
            CallableStatement cs = conn.prepareCall("{call Man_delete_MeActPerFecEjecucion(?)}");
            cs.setInt(1, bean.getNUM_CODACTPER_PK());           
            cs.execute();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.toString());
            msj = e.toString();
            return msj;
        }
        return msj;
    }
       public String spManUpdateInsertObservacion(AvancesPorActividadVO bean) {
        String msj = "";

        try {

            conn = DriverManager.getConnection(url, usuario, password);
            CallableStatement cs = conn.prepareCall("{call Man_updateInsertObservacion(?,?)}");
            cs.setInt(1, bean.getNUM_CODACTPER_PK());
            cs.setString(2, bean.getTX_OBSERVACION());
           
            cs.execute();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.toString());
            msj = e.toString();
            return msj;
        }
        return msj;
    }

   
}
